package com.jmdz.fmis.admin.api.address;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.jmdz.fmis.core.exception.AdminServiceException;
import com.jmdz.fmis.core.exception.ExceptionDefinition;
import com.jmdz.fmis.core.exception.ServiceException;
import com.jmdz.fmis.data.model.Page;
import com.jmdz.fmis.data.domain.AddressDO;
import com.jmdz.fmis.data.mapper.AddressMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.apache.ibatis.session.RowBounds;
import java.util.Date;
import java.util.List;

/**
 * Generate Code By Fmis
 */
@Service
public class AdminaddressServiceImpl implements AdminAddressService {

    @Autowired
    private AddressMapper addressMapper;

    @Override
    public boolean delete(Long id,  Long adminId) throws ServiceException {
        return addressMapper.deleteById(id) > 0;
    }

    @Override
    public Page<AddressDO> list(String province, Integer page, Integer limit, Long adminId) throws ServiceException {
    QueryWrapper<AddressDO> wrapper = new QueryWrapper<AddressDO>();
        if (province != null) {
            wrapper.like("province", province);
        }
        return addressMapper.selectPage(Page.div(page, limit, AddressDO.class), wrapper);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public AddressDO create(String province, Long adminId) throws ServiceException {
         Date now = new Date();
         AddressDO insertDO = new AddressDO();
         insertDO.setProvince(province);
         insertDO.setGmtUpdate(now);
         insertDO.setGmtCreate(now);
         if (addressMapper.insert(insertDO) > 0) {
             return insertDO;
         }
         throw new AdminServiceException(ExceptionDefinition.ADMIN_UNKNOWN_EXCEPTION);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public String edit(Long id, String province, Long adminId) throws ServiceException {
         Date now = new Date();
         AddressDO updateDO = new AddressDO();
         updateDO.setId(id);
         updateDO.setProvince(province);
         updateDO.setGmtUpdate(now);
         if (addressMapper.updateById(updateDO) > 0) {
             return "ok";
         }
         throw new AdminServiceException(ExceptionDefinition.ADMIN_UNKNOWN_EXCEPTION);
    }

}
